﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client.Indexes;
using LogManager.Base;
using LogManager.Base.Models.Projections;

namespace LogManager.LocalStorage.Indexes
{
    public class Events_LevelsPerDate : AbstractIndexCreationTask<Event,Base.Models.Projections.LevelsPerDateProjection>
    {
        public Events_LevelsPerDate()
        {
            Map = events =>
                  from e in events
                  select new
                             {
                                 LogName = e.LogName,
                                 Date = e.TimeStamp.Date,
                                 Critical = e.Level == "Critical" ? 1 : 0,
                                 Warning = e.Level == "Warning" ? 1 : 0,
                                 Error = e.Level == "Error" ? 1 : 0,
                                 Information = e.Level == "Information" ? 1 : 0
                             };
            Reduce = levels =>
                     from l in levels
                     group l by new { l.LogName, l.Date }
                         into g
                         select new
                                    {
                                        LogName = g.Key.LogName,
                                        Date = g.Key.Date,
                                        Critical = g.Sum(x => x.Critical),
                                        Warning = g.Sum(x => x.Warning),
                                        Error = g.Sum(x => x.Error),
                                        Information = g.Sum(x => x.Information)
                                    };
        }
    }
}
